<script setup lang="ts">
import {computed, onBeforeMount, ref} from "vue";
import {useSystemDictStore} from "@/store/system/systemDictStore";
import {report} from "@/api/user/user";
import {ECode} from "@/util/commonUtil";
import {message} from "ant-design-vue";
import {userConfigStore} from "@/store/user/userConfigStore";

const props = defineProps({
  userInfo: {
    type: Object,
  },
  visible: {
    type: Boolean,
    default: false
  }
});
const form = ref<any>({
  violationType: "",
  content: "",
})
const commentReportDictList = ref<any>([])
const open = ref<boolean>(props.visible)
const reportRequest = ref<any>({})
const emit = defineEmits(['beforeClose'])

onBeforeMount(() => {
  getDictList()
})

function getDictList() {
  let commentReportDict: any = computed(() => useSystemDictStore().SysContentReportType);
  if (commentReportDict.value) {
    commentReportDictList.value = commentReportDict.value;
  }
}

function cancel() {
  emit('beforeClose', false)
}

async function submit() {
  let userInfo: any = computed(() => userConfigStore().userInfo)
  let userUid = userInfo.uid
  let reportInfo: any = reportRequest.value
  if (userInfo.userName == undefined) {
    message.warning('登录后才能举报评论哦~')
    return
  }
  if (userUid == reportInfo.userUid) {
    message.warning('不能举报自己的评论哦~')
    return
  }
  if (form.value.content==""){
    message.warning('请输入举报原因')
    return
  }
  reportInfo.violationType = form.value.violationType
  reportInfo.content = form.value.content
  await report(reportInfo).then((response: any) => {
    if (response.code == ECode.SUCCESS) {
      message.success('举报成功')
      cancel()
    } else {
      message.success(response.message)
    }
  })
}
</script>

<template>
  <a-modal v-model:open="open" title="举报反馈" @cancel="cancel" @ok="submit">
    <a-form :model="form">
      <a-row>
        <a-col :span="24">
          <a-form-item label="举报类型">
            <a-radio-group v-model:value="form.violationType">
              <template v-for="item in commentReportDictList" :key="item.uid">
                <a-radio :value="item.dictLabel" :option-type="item.listClass">{{ item.dictLabel }}</a-radio>
              </template>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="举报内容" name="content"
                       :rules="[{ required: true, message: '举报内容为空!',trigger: 'blur' },
                       {min: 1, max: 255, message: '长度在1到255个字符'}]">
            <a-textarea
                :rows="4"
                placeholder="请详细描述举报原因，我们将第一时间核实处理！"
                v-model:value.trim="form.content">
            </a-textarea>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<style scoped lang="less">

</style>
